package com.mhkj.base.itemInfo.tree.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.itemInfo.building.entity.Building;
import com.mhkj.base.itemInfo.building.service.BuildingService;
import com.mhkj.base.itemInfo.group.entity.Group;
import com.mhkj.base.itemInfo.group.service.GroupService;
import com.mhkj.base.itemInfo.item.entity.Item;
import com.mhkj.base.itemInfo.item.service.ItemService;
import com.mhkj.base.itemInfo.phase.entity.Phase;
import com.mhkj.base.itemInfo.phase.service.PhaseService;
import com.mhkj.base.itemInfo.tree.vo.TreeItemInfoVo;
import com.mhkj.base.region.service.NationService;
import com.mics.core.system.organizational.bean.Company;
import com.mics.core.system.organizational.bean.CompanyData;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;

@Controller
@RequestMapping("/tree/itemInfo")
public class TreeItemInfoController {

	@Autowired
	private BuildingService buildingService;
	@Autowired
	private NationService nationService;
	@Autowired
	private PhaseService phaseService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private ItemService itemService;
	@Autowired
	private GroupService groupService;
	
	
	/**
	 * 查询所有项目期区树
	 * @throws Exception 
	 * @throws IllegalAccessException 
	 * 
	 */
	@RequestMapping(value="/list", method=RequestMethod.POST)
	@ResponseBody
	public List<TreeItemInfoVo> list(Long cityId,String itemName,Long provinceId) throws Exception {
	
		//获取所有项目
		 List<TreeItemInfoVo> listVo = new ArrayList<TreeItemInfoVo>();
		String CompanyId = null;
		Integer status = null;
		List<Item>  list = itemService.getListItem(CompanyId,itemName,status,cityId,provinceId);
		
		for (Item item : list) {
			 
			String itemcodeId = "@"+item.hashCode();
			
			TreeItemInfoVo  itemVO = new TreeItemInfoVo();
			itemVO.setCreateDate(item.getCreateDate());
			Employee employeeInfo = orgService.employeeInfo(item.getCreateBy());
			itemVO.setCreateName(employeeInfo.getName());
			itemVO.setId(item.getId()+itemcodeId);
			itemVO.setName(item.getName());
			itemVO.setPid(null);
			itemVO.setType("item");
			listVo.add(itemVO);
			//查询该项项目下的所组团
			
			List<Group> groupByItemId = groupService.getGroupByItemId(item.getId());
			
			for (Group group : groupByItemId) {
				
				String groupcodeId = "@"+group.hashCode();
				
				itemVO.setStatus(1);
				TreeItemInfoVo  groupVo = new TreeItemInfoVo();
				groupVo.setCreateDate(group.getCreateDate());
				Employee empgroup = orgService.employeeInfo(group.getCreateBy());
				groupVo.setCreateName(empgroup.getName());
				groupVo.setId(group.getId()+groupcodeId);
				groupVo.setName(group.getGroupName());
				groupVo.setPid(item.getId()+itemcodeId);
				groupVo.setType("group");
				listVo.add(groupVo);
				//查询组团下面的期区
				List<Phase> findPhaseByGroupId = phaseService.findPhaseByGroupId(group.getId());
				for (Phase phase : findPhaseByGroupId) {
					String phasecodeId = "@"+phase.hashCode();
					
					groupVo.setStatus(1);
					TreeItemInfoVo  groupphaseVo = new TreeItemInfoVo();
					groupphaseVo.setCreateDate(phase.getCreateDate());
					Employee empgroupphase = orgService.employeeInfo(phase.getCreateBy());
					groupphaseVo.setCreateName(empgroupphase.getName());
					groupphaseVo.setId(phase.getId()+phasecodeId);
					groupphaseVo.setName(phase.getPhaseName());
					groupphaseVo.setPid(group.getId()+groupcodeId);
					groupphaseVo.setType("phase");
					listVo.add(groupphaseVo);
					//查询期区下面的楼栋
					List<Building> listBuild = buildingService.findBuildByPhaseId(phase.getId());
					for (Building building : listBuild) {
						
						String buildingcodeId = "@"+building.hashCode();
						
						groupphaseVo.setStatus(1);
						TreeItemInfoVo  buildingVo = new TreeItemInfoVo();
						buildingVo.setCreateDate(building.getCreateDate());
						Employee empgbuild = orgService.employeeInfo(phase.getCreateBy());
						buildingVo.setCreateName(empgbuild.getName());
						buildingVo.setId(building.getId()+buildingcodeId);
						buildingVo.setName(building.getBuildingName());
						buildingVo.setPid(phase.getId()+phasecodeId);
						buildingVo.setType("build");
						listVo.add(buildingVo);
					}
				}
			}
			//查询该项目下的非组团期区
			List<Phase> findPhaseByItemId = phaseService.findPhaseByItemId(item.getId());
			for (Phase phase : findPhaseByItemId) {
				
				String phasecodeId = "@"+phase.hashCode();
				
				itemVO.setStatus(1);
				TreeItemInfoVo  phaseVo = new TreeItemInfoVo();
				phaseVo.setCreateDate(phase.getCreateDate());
				Employee empphase = orgService.employeeInfo(phase.getCreateBy());
				phaseVo.setCreateName(empphase.getName());
				phaseVo.setId(phase.getId()+phasecodeId);
				phaseVo.setName(phase.getPhaseName());
				phaseVo.setPid(item.getId()+itemcodeId);
				phaseVo.setType("phase");
				listVo.add(phaseVo);
				//查询期区下面的楼栋
				List<Building> listBuild = buildingService.findBuildByPhaseId(phase.getId());
				for (Building building : listBuild) {
					
					String buildingcodeId = "@"+building.hashCode();
					
					phaseVo.setStatus(1);
					TreeItemInfoVo  buildingVo = new TreeItemInfoVo();
					buildingVo.setCreateDate(building.getCreateDate());
					Employee empgbuild = orgService.employeeInfo(phase.getCreateBy());
					buildingVo.setCreateName(empgbuild.getName());
					buildingVo.setId(building.getId()+buildingcodeId);
					buildingVo.setName(building.getBuildingName());
					buildingVo.setPid(phase.getId()+phasecodeId);
					buildingVo.setType("build");
					
					listVo.add(buildingVo);
				}
				
			}
			
		}
		
	
	
	return listVo;
	}
	
	
	/**
	 * 获取所有公司下面的项目树到楼栋
	 */
	@RequestMapping(value="/findTreeToBuild", method=RequestMethod.POST)
	@ResponseBody
	public List<TreeItemInfoVo> findTreeToBuild() {
		 
		CompanyData companiedData =  orgService.listCompanys();
		if(companiedData!=null) {
		List<Company> childCompanyList = companiedData.getChildCompanyList();
		List<TreeItemInfoVo> listVo = new ArrayList<TreeItemInfoVo>();
		TreeItemInfoVo e = new TreeItemInfoVo();
		e.setId(companiedData.getHeadCompany().getId());
		e.setName(companiedData.getHeadCompany().getCompanyname());
		listVo.add(e);
		for (Company company : childCompanyList) {
			
			String buildingcodeId = "@"+company.hashCode();
			e.setStatus(1);
			TreeItemInfoVo en = new TreeItemInfoVo();
			String id = company.getId();
			en.setId(id+buildingcodeId);
			en.setType("company");
			en.setPid(company.getParentid());
			en.setName(company.getCompanyname());
			List<Item>	listItem = itemService.getListItemByaffiliatedCompanyId(id,null,null);
			for (Item item : listItem) {
				 
				String itemcodeId = "@"+item.hashCode();
				TreeItemInfoVo  itemVO = new TreeItemInfoVo();
				en.setStatus(1);
				itemVO.setCreateDate(item.getCreateDate());
				Employee employeeInfo = orgService.employeeInfo(item.getCreateBy());
				itemVO.setCreateName(employeeInfo.getName());
				itemVO.setId(item.getId()+itemcodeId);
				itemVO.setName(item.getName());
				itemVO.setPid(id+buildingcodeId);
				itemVO.setType("item");
				listVo.add(itemVO);
				//查询该项项目下的所组团
				
				List<Group> groupByItemId = groupService.getGroupByItemId(item.getId());
				
				for (Group group : groupByItemId) {
					
					String groupcodeId = "@"+group.hashCode();
					
					itemVO.setStatus(1);
					TreeItemInfoVo  groupVo = new TreeItemInfoVo();
					groupVo.setCreateDate(group.getCreateDate());
					Employee empgroup = orgService.employeeInfo(group.getCreateBy());
					groupVo.setCreateName(empgroup.getName());
					groupVo.setId(group.getId()+groupcodeId);
					groupVo.setName(group.getGroupName());
					groupVo.setPid(item.getId()+itemcodeId);
					groupVo.setType("group");
					listVo.add(groupVo);
					//查询组团下面的期区
					List<Phase> findPhaseByGroupId = phaseService.findPhaseByGroupId(group.getId());
					for (Phase phase : findPhaseByGroupId) {
						String phasecodeId = "@"+phase.hashCode();
						
						groupVo.setStatus(1);
						TreeItemInfoVo  groupphaseVo = new TreeItemInfoVo();
						groupphaseVo.setCreateDate(phase.getCreateDate());
						Employee empgroupphase = orgService.employeeInfo(phase.getCreateBy());
						groupphaseVo.setCreateName(empgroupphase.getName());
						groupphaseVo.setId(phase.getId()+phasecodeId);
						groupphaseVo.setName(phase.getPhaseName());
						groupphaseVo.setPid(group.getId()+groupcodeId);
						groupphaseVo.setType("phase");
						listVo.add(groupphaseVo);
						//查询期区下面的楼栋
						List<Building> listBuild = buildingService.findBuildByPhaseId(phase.getId());
						for (Building building : listBuild) {
							
							String buildingcodeId1 = "@"+building.hashCode();
							
							groupphaseVo.setStatus(1);
							TreeItemInfoVo  buildingVo = new TreeItemInfoVo();
							buildingVo.setCreateDate(building.getCreateDate());
							Employee empgbuild = orgService.employeeInfo(phase.getCreateBy());
							buildingVo.setCreateName(empgbuild.getName());
							buildingVo.setId(building.getId()+buildingcodeId1);
							buildingVo.setName(building.getBuildingName());
							buildingVo.setPid(phase.getId()+phasecodeId);
							buildingVo.setType("build");
							listVo.add(buildingVo);
						}
					}
				}
				//查询该项目下的非组团期区
				List<Phase> findPhaseByItemId = phaseService.findPhaseByItemId(item.getId());
				for (Phase phase : findPhaseByItemId) {
					
					String phasecodeId = "@"+phase.hashCode();
					
					itemVO.setStatus(1);
					TreeItemInfoVo  phaseVo = new TreeItemInfoVo();
					phaseVo.setCreateDate(phase.getCreateDate());
					Employee empphase = orgService.employeeInfo(phase.getCreateBy());
					phaseVo.setCreateName(empphase.getName());
					phaseVo.setId(phase.getId()+phasecodeId);
					phaseVo.setName(phase.getPhaseName());
					phaseVo.setPid(item.getId()+itemcodeId);
					phaseVo.setType("phase");
					listVo.add(phaseVo);
					//查询期区下面的楼栋
					List<Building> listBuild = buildingService.findBuildByPhaseId(phase.getId());
					for (Building building : listBuild) {
						
						String buildingcodeId2 = "@"+building.hashCode();
						
						phaseVo.setStatus(1);
						TreeItemInfoVo  buildingVo = new TreeItemInfoVo();
						buildingVo.setCreateDate(building.getCreateDate());
						Employee empgbuild = orgService.employeeInfo(phase.getCreateBy());
						buildingVo.setCreateName(empgbuild.getName());
						buildingVo.setId(building.getId()+buildingcodeId2);
						buildingVo.setName(building.getBuildingName());
						buildingVo.setPid(phase.getId()+phasecodeId);
						buildingVo.setType("build");
						
						listVo.add(buildingVo);
					}
					
				}
				
			}
			listVo.add(en);
		}
		
		return listVo;
		}else {
			return null;
		}
	}
 
	
	
	
	
	
}
